package com.epam.training;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.epam.training.domain.Paymant;
import com.epam.training.service.bookingitem.BookingItemService;
import com.epam.training.service.paymant.PaymantService;

@Controller
@RequestMapping("/customer/paid")
public class NewPaymantController {

	@Autowired
	private PaymantService paymantService;

	@Autowired
	private BookingItemService bookingService;

	@ModelAttribute
	private Paymant createPaymant(@RequestParam BigDecimal pPaid) {
		Paymant paymant = new Paymant();
		paymant.setPaid(pPaid);
		paymant.setPaymantDate(new Date());

		return paymant;
	}

	@RequestMapping
	private String savePaymant(@ModelAttribute Paymant paymant,
			@RequestParam Integer pBookingItemId) {
		Paymant p = paymantService.save(paymant);
		bookingService.addPaymant(pBookingItemId, p.getPaymantId());

		return "redirect:/customer";
	}

}
